from PIL import ImageFilter, ImageQt, Image
from PIL.ImageFile import ImageFile

from PySide6 import QtWidgets, QtCore
from PySide6.QtCore import QPoint
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QLabel, QSlider, QPushButton, QFileDialog


class MyWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.blurPic = None
        self.img = None
        self.btn = QPushButton()
        self.btn.clicked.connect(self.get_img)

        self.imgLabel = QLabel()
        self.imgLabel.resize(300, 100)

        self.slider = QSlider(QtCore.Qt.Horizontal, self)
        self.slider.setRange(0, 20)
        self.slider.setTickPosition(QSlider.TickPosition.TicksBelow)
        self.slider.setTickInterval(2)
        self.slider.valueChanged.connect(self.slider_value_changed)

        self.mainLayout = QtWidgets.QVBoxLayout()
        self.mainLayout.addWidget(self.btn)
        self.mainLayout.addWidget(self.imgLabel)
        self.mainLayout.addWidget(self.slider)
        self.setLayout(self.mainLayout)

    def get_img(self):
        try:
            self.img = Image.open(
                QFileDialog.getOpenFileName(self, 'chose image', './', 'Image Files (*.jpg *.jpeg *.png)')[0])
            self.imgLabel.setPixmap(ImageQt.toqpixmap(self.img))
        except Exception as e:
            print(e)

    def slider_value_changed(self, value):
        if self.img is not None:
            self.blurPic = self.img.filter(ImageFilter.GaussianBlur(value))
            self.imgLabel.setPixmap(ImageQt.toqpixmap(self.blurPic))
        else:
            print('no image chosen')


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = MyWindow()
    window.show()
    app.exec()
